/**
 * SPEA2_main.java
 *
 * @author Juan J. Durillo
 * @version 1.0
 */
package jmetal.metaheuristics.spea2;

import jmetal.base.*;
import jmetal.base.operator.crossover.*   ;
import jmetal.base.operator.mutation.*    ; 
import jmetal.base.operator.selection.*   ;
import jmetal.problems.*                  ;
import jmetal.problems.DTLZ.*;
import jmetal.problems.ZDT.*;
import jmetal.problems.WFG.*;
import jmetal.problems.LZ09.* ;
import jmetal.qualityIndicator.QualityIndicator;

import jmetal.util.Configuration;
import jmetal.util.JMException;
import java.io.IOException;

import java.util.logging.FileHandler;
import java.util.logging.Logger;

import P3.*;
import jmetal.qualityIndicator.QualityIndicator;

public class SPEA2_main2 {
		  public static Logger      logger_ ;      // Logger object
		  public static FileHandler fileHandler_ ; // FileHandler object
  
  /**
   * @param args Command line arguments. The first (optional) argument specifies 
   *             the problem to solve.
   * @throws JMException 
   * @throws IOException 
   * @throws SecurityException 
   * Usage: three options
   *      - jmetal.metaheuristics.mocell.MOCell_main
   *      - jmetal.metaheuristics.mocell.MOCell_main problemName
   *      - jmetal.metaheuristics.mocell.MOCell_main problemName ParetoFrontFile
   */
  public void run(String question, int fix) throws 
  								JMException, 
  								IOException, 
  								ClassNotFoundException {
    Problem   problem   ;         // The problem to solve
    Algorithm algorithm ;         // The algorithm to use
    Operator  crossover ;         // Crossover operator
    Operator  mutation  ;         // Mutation operator
    Operator  selection ;         // Selection operator
        
    QualityIndicator indicators ; // Object to get quality indicators

    // Logger object and file to store log messages
    logger_      = Configuration.logger_ ;
    fileHandler_ = new FileHandler("SPEA2.log"); 
    logger_.addHandler(fileHandler_) ;
    
    indicators = null ;

    problem = new Cycling2(question);//changeable 

       
    algorithm = new SPEA2(problem);
    
    // Algorithm parameters
    algorithm.setInputParameter("populationSize",10);
    algorithm.setInputParameter("maxEvaluations",20000);
    algorithm.setInputParameter("archiveSize",10);
      
    //determine to make transition fix or not
    if(fix==1)
    {
	    // Mutation and Crossover for Real codification 
	    crossover = CrossoverFactory.getCrossoverOperator("MyCrossover2");                   
	    crossover.setParameter("probability",0.9);                   
	    //crossover.setParameter("distributionIndex",20.0);
	
	    mutation = MutationFactory.getMutationOperator("MyMutation2");                    
	    mutation.setParameter("probability",0.2);
	    //mutation.setParameter("distributionIndex",20.0);
    }else{
	    // Mutation and Crossover for Real codification 
	    crossover = CrossoverFactory.getCrossoverOperator("MyCrossover");                   
	    crossover.setParameter("probability",0.9);                   
	    //crossover.setParameter("distributionIndex",20.0);
	
	    mutation = MutationFactory.getMutationOperator("MyMutation");                    
	    mutation.setParameter("probability",0.2);
	    //mutation.setParameter("distributionIndex",20.0);
    }
        
    // Selection operator 
    selection = SelectionFactory.getSelectionOperator("BinaryTournament") ;                            
    
    // Add the operators to the algorithm
    algorithm.addOperator("crossover",crossover);
    algorithm.addOperator("mutation",mutation);
    algorithm.addOperator("selection",selection);
    
    // Add the indicator object to the algorithm
    algorithm.setInputParameter("indicators", indicators) ;
    
    // Execute the algorithm
    long initTime = System.currentTimeMillis();
    SolutionSet population = algorithm.execute();
    long estimatedTime = System.currentTimeMillis() - initTime;

    // Result messages 
    logger_.info("Total execution time: "+estimatedTime + "ms");
    logger_.info("Variables values have been writen to file VAR");
    population.printVariablesToFile("VAR");    
    logger_.info("Objectives values have been writen to file FUN");
    population.printObjectivesToFile("FUN");     
    
    /*if (indicators != null) {
      logger_.info("Quality indicators") ;
      logger_.info("Hypervolume: " + indicators.getHypervolume(population)) ;
      logger_.info("GD         : " + indicators.getGD(population)) ;
      logger_.info("IGD        : " + indicators.getIGD(population)) ;
      logger_.info("Spread     : " + indicators.getSpread(population)) ;
      logger_.info("Epsilon    : " + indicators.getEpsilon(population)) ;
    } */
    // if 
  }//main
} // SPEA2_main.java
